package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;


    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
   log.info("开始登录了...{}",employeeLoginDTO);
   //1.登录返回一个员工信息
   Employee employee= employeeService.login(employeeLoginDTO);
   //2.生成令牌
   String token = JwtUtil.getToken(employee.getId());
        //3.返回所需的JSON
       EmployeeLoginVO employeeLoginVO=EmployeeLoginVO.builder()
               .id(employee.getId())
               .name(employee.getName())
               .userName(employee.getUsername())
               .token(token)
               .build();
        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * @param employeeDTO
     * */
    @PostMapping
    public Result save(@RequestBody EmployeeDTO employeeDTO){
      employeeService.save(employeeDTO);
     return Result.success();
    }

}
